package com.atguigu.wordcount.flowbean;

import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * @ClassName FlowMapper
 * @DeScription FlowMapper
 * @Author 剧情再美终是戏
 * @Date 2019/11/18 17:05
 * @Version 1.0
 **/
public class FlowMapper extends Mapper<LongWritable, Text, Text, FlowBean> {

    private FlowBean flowBean = new FlowBean();
    private Text out_key = new Text();

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String line = "";
        String[] split = new String[0];
        // 获取一行
        if (null != value) {
            line = value.toString();
        }

        // 分割数据
        if (StringUtils.isNotBlank(line)) {
            split = line.split("\t");
        }

        // 组装数据
        if (null != split && split.length > 0) {
            long uploadSum = Long.parseLong(split[split.length - 3]);
            long downSum = Long.parseLong(split[split.length - 2]);
            flowBean.setDownSum(downSum);
            flowBean.setUploadSum(uploadSum);
            flowBean.setTotal(uploadSum + downSum);
            out_key.set(split[1]);
        }

        // 写出数据

        context.write(out_key, flowBean);
    }
}
